namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MenuBLL
    {
        public int Add(Menu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Menu(");
            builder.Append("MenuName,MenuCode");
            builder.Append(") values (");
            builder.Append("@MenuName,@MenuCode");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuName", model.MenuName), new SqlParameter("@MenuCode", model.MenuCode) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Menu ");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MenuID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MenuIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Menu ");
            builder.Append(" where ID in (" + MenuIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<Menu> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM Menu ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            Menu item = null;
            List<Menu> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<Menu>();
                foreach (DataRow row in table.Rows)
                {
                    item = new Menu();
                    if (row["MenuID"].ToString() != "")
                    {
                        item.MenuID = int.Parse(row["MenuID"].ToString());
                    }
                    item.MenuName = row["MenuName"].ToString();
                    item.MenuCode = row["MenuCode"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public Menu GetModel(int MenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuID, MenuName, MenuCode  ");
            builder.Append("  from Menu ");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MenuID;
            Menu menu = new Menu();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["MenuID"].ToString() != "")
                {
                    menu.MenuID = int.Parse(row["MenuID"].ToString());
                }
                menu.MenuName = row["MenuName"].ToString();
                menu.MenuCode = row["MenuCode"].ToString();
                return menu;
            }
            return null;
        }

        public bool Update(Menu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Menu set ");
            builder.Append(" MenuName = @MenuName , ");
            builder.Append(" MenuCode = @MenuCode  ");
            builder.Append(" where MenuID=@MenuID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", model.MenuID), new SqlParameter("@MenuName", model.MenuName), new SqlParameter("@MenuCode", model.MenuCode) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

